package com.example.util;

import javax.servlet.http.HttpServletRequest;

import com.example.sec.form.UserInfoEditForm;
import com.example.sec.form.UserSignupForm;

public class ParaValidator {
	public static boolean validateParameters(HttpServletRequest request, UserInfoEditForm userSignupForm){
		String userName = userSignupForm.getUserName();
		String email = userSignupForm.getEmail();
		
		String password = userSignupForm.getPassword();

		if (!validateStringLength(userName, SecConstant.NAME_LENGTH)){
			request.setAttribute(SecConstant.ERROR_MESSAGE, 
					"The username is too long,it's length should not be over 20.");
			return false;
		}
		
		if (!validateStringLength(email, SecConstant.EMAIL_LENGTH)){
			request.setAttribute(SecConstant.ERROR_MESSAGE, 
					"The email is too long,it's length should not be over 20.");
			return false;
		}
		
		if (!validateStringLength(password, SecConstant.PASSWD_LENGTH)){
			request.setAttribute(SecConstant.ERROR_MESSAGE, 
					"The password is too long,it's length should not be over 20.");
			return false;
		}
		
		String firstName = userSignupForm.getFirstName();
		String lastName  = userSignupForm.getLastName();
		String phoneNumber = userSignupForm.getPhoneNumber();
		String address = userSignupForm.getAddress();
		
		if (firstName != null && !validateStringLength(firstName, SecConstant.FIRSTNAME_LENGTH)){
			request.setAttribute(SecConstant.ERROR_MESSAGE, 
					"The firstname is too long,it's length should not be over 20.");
			return false;
		}
		
		if (lastName != null && !validateStringLength(lastName, SecConstant.LASTNAME_LENGTH)){
			request.setAttribute(SecConstant.ERROR_MESSAGE, 
					"The lastName is too long,it's length should not be over 20.");
			return false;
		}
		
		if (phoneNumber != null && !validateStringLength(phoneNumber, SecConstant.PHONE_NUMBLE_LENGTH)){
			request.setAttribute(SecConstant.ERROR_MESSAGE, 
					"The phone number is too long,it's length should not be over 50.");
			return false;
		}
		
		if (address != null && !validateStringLength(address, SecConstant.ADDRESS_LENGTH)){
			request.setAttribute(SecConstant.ERROR_MESSAGE, 
					"The address is too long,it's length should not be over 255.");
			return false;
		}
		
		return true;
	}
	
	public static boolean validateParameters(HttpServletRequest request, UserSignupForm userSignupForm){
		String userName = userSignupForm.getUserName();
		String email = userSignupForm.getEmail();
		
		String password = userSignupForm.getPassword();

		if (!validateStringLength(userName, SecConstant.NAME_LENGTH)){
			request.setAttribute(SecConstant.ERROR_MESSAGE, 
					"The username is too long,it's length should not be over 20.");
			return false;
		}
		
		if (!validateStringLength(email, SecConstant.EMAIL_LENGTH)){
			request.setAttribute(SecConstant.ERROR_MESSAGE, 
					"The email is too long,it's length should not be over 20.");
			return false;
		}
		
		if (!validateStringLength(password, SecConstant.PASSWD_LENGTH)){
			request.setAttribute(SecConstant.ERROR_MESSAGE, 
					"The password is too long,it's length should not be over 20.");
			return false;
		}
		
		String firstName = userSignupForm.getFirstName();
		String lastName  = userSignupForm.getLastName();
		String phoneNumber = userSignupForm.getPhoneNumber();
		String address = userSignupForm.getAddress();
		
		if (firstName != null && !validateStringLength(firstName, SecConstant.FIRSTNAME_LENGTH)){
			request.setAttribute(SecConstant.ERROR_MESSAGE, 
					"The firstname is too long,it's length should not be over 20.");
			return false;
		}
		
		if (lastName != null && !validateStringLength(lastName, SecConstant.LASTNAME_LENGTH)){
			request.setAttribute(SecConstant.ERROR_MESSAGE, 
					"The lastName is too long,it's length should not be over 20.");
			return false;
		}
		
		if (phoneNumber != null && !validateStringLength(phoneNumber, SecConstant.LASTNAME_LENGTH)){
			request.setAttribute(SecConstant.ERROR_MESSAGE, 
					"The phone number is too long,it's length should not be over 20.");
			return false;
		}
		
		if (address != null && !validateStringLength(address, SecConstant.ADDRESS_LENGTH)){
			request.setAttribute(SecConstant.ERROR_MESSAGE, 
					"The address is too long,it's length should not be over 255.");
			return false;
		}
		
		return true;
	}
	
	private static boolean validateStringLength(String value, int length){
		if (value.length() > length){
			return false;
		}
		return true;
	}
}
